﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BalanceCenter_NetCopy : PageBase
{
    public string[] MemberBack = new string[7];
    public string[] NextID = new string[7];

    protected void Page_Load(object sender, EventArgs e)
    {
        long id = ShoveEL._Convert.StrToLong(this.Request["id"], -1);
        if (id < 0)
        {
            id = _User.ID;
        }

        if ((id < _User.ID) && (!_User.Competences[Competences.LookNet]))
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        //if (_User.ID == 1)
        //{
        //    trSearch.Visible = true;
        //}

        BindData(id);
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        SecondLevelPassword = false;    //是否需要二级登录

        RequestLoginPage = "BalanceCenter/Net.aspx";

        base.OnInit(e);
    }

    #endregion

    private void BindData(long id)
    {
        DataSet ds = DAL.Procedures.P_GetNet.Open(id);

        if (ds == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        if (ds.Tables.Count == 0)
        {
            PublicFunction.GoError(ErrorNumber.NoData, "暂无数据", this.GetType().BaseType.FullName);

            return;
        }

        DataTable dt = ds.Tables[0];

        if (dt.Columns.Count == 0)
        {
            PublicFunction.GoError(ErrorNumber.NoData, "暂无数据", this.GetType().BaseType.FullName);

            return;
        }

        for (int i = 0; i < 7; i++)
        {
            DataRow[] dr = dt.Select("Locate=" + (i + 1).ToString());
            if (dr.Length < 1)
            {
                MemberBack[i] = "#FFFFFF";
            }
            else
            {
                ((Panel)this.FindControl("Panel" + (i + 1).ToString())).Visible = true;

                long UserID = ShoveEL._Convert.StrToLong(dr[0]["id"].ToString(), -1);
                if (UserID == -1)
                {
                    MemberBack[i] = "#FFFFFF";
                    ((Label)this.FindControl("labName" + (i + 1).ToString())).Text = "<a class='li8' href='Buy.aspx?PID=" + dr[0]["ParentID"].ToString() + "&LR=" + dr[0]["LR"].ToString() + "'>空位推荐</a>";
                }
                else
                {
                    string UserTypeName = "";

                    int UserType = ShoveEL._Convert.StrToInt(dr[0]["UserType"].ToString(), 1);
                    int isActivation = ShoveEL._Convert.StrToInt(dr[0]["isActivation"].ToString(), 0);

                    if (UserType == 1)
                    {
                        UserTypeName = "银级店";
                    }
                    else if (UserType == 2)
                    {
                        UserTypeName = "金级店";
                    }
                    else if (UserType == 3)
                    {
                        UserTypeName = "白金店";
                    }
                    else if (UserType == 4)
                    {
                        UserTypeName = "钻石店";
                    }

                    if (isActivation == 1)
                    {
                        MemberBack[i] = "#fad6d8";
                    }
                    else if (isActivation == 0)
                    {
                        MemberBack[i] = "#f8f7c9";
                    }

                    DataTable dt2 = DAL.Tables.T_Users.Select("", "ID=" + UserID, "");
                    string aaa = dt2.Rows[0]["isSurrogate"].ToString();

                    bool isSurrogate = ShoveEL._Convert.StrToBool(DAL.Tables.T_Users.Select("", "ID=" + UserID, "").Rows[0]["isSurrogate"].ToString(), false);
                    if (isSurrogate)
                    {
                        //((Label)this.FindControl("labSurrogate" + (i + 1).ToString())).Text = "<a class='li8'>(代理商)</a>";
                    }
                    else
                    {                        
                    }

                    //((Label)this.FindControl("labBDZX" + (i + 1).ToString())).Text = (ShoveEL._Convert.StrToBool(dr[0]["isSurrogate"].ToString(), false) ? "<img src='../Images/Surrogate.gif' border='0'>" : "");
                    ((Label)this.FindControl("labBDZX" + (i + 1).ToString())).Text = "<a class='li8' href='NetCopy.aspx?id=" + dr[0]["id"].ToString() + "'>" + dr[0]["Name"].ToString() + "</a>";
                    ((Label)this.FindControl("labName" + (i + 1).ToString())).Text = "<a class='li8'>(" + UserTypeName + ")</a>";

                    double L = 0;
                    double R = 0;
                    double L_CurrentMonth = 0;
                    double R_CurrentMonth = 0;
                    double L_CurrentDay = 0;
                    double R_CurrentDay = 0;
                    double L_Residual = 0;
                    double R_Residual = 0;

                    if (DAL.Procedures.P_GetScoreForUser.Call(ShoveEL._Convert.StrToLong(dr[0]["id"].ToString(), -1), ref L, ref R, ref L_CurrentMonth, ref R_CurrentMonth, ref L_CurrentDay, ref R_CurrentDay, ref L_Residual, ref R_Residual) < 0)
                    {
                        ((Label)this.FindControl("labSL" + (i + 1).ToString())).Text = "0";
                        ((Label)this.FindControl("labSR" + (i + 1).ToString())).Text = "0";
                        ((Label)this.FindControl("labSL" + (i + 1).ToString() + "_1")).Text = "0";
                        ((Label)this.FindControl("labSR" + (i + 1).ToString() + "_1")).Text = "0";
                        ((Label)this.FindControl("labSL" + (i + 1).ToString() + "_2")).Text = "0";
                        ((Label)this.FindControl("labSR" + (i + 1).ToString() + "_2")).Text = "0";
                        ((Label)this.FindControl("labSL" + (i + 1).ToString() + "_3")).Text = "0";
                        ((Label)this.FindControl("labSR" + (i + 1).ToString() + "_3")).Text = "0";
                    }
                    else
                    {
                        ((Label)this.FindControl("labSL" + (i + 1).ToString())).Text = L.ToString();
                        ((Label)this.FindControl("labSR" + (i + 1).ToString())).Text = R.ToString();
                        ((Label)this.FindControl("labSL" + (i + 1).ToString() + "_1")).Text = L_CurrentMonth.ToString();
                        ((Label)this.FindControl("labSR" + (i + 1).ToString() + "_1")).Text = R_CurrentMonth.ToString();
                        ((Label)this.FindControl("labSL" + (i + 1).ToString() + "_2")).Text = L_CurrentDay.ToString();
                        ((Label)this.FindControl("labSR" + (i + 1).ToString() + "_2")).Text = R_CurrentDay.ToString();
                        ((Label)this.FindControl("labSL" + (i + 1).ToString() + "_3")).Text = L_Residual.ToString();
                        ((Label)this.FindControl("labSR" + (i + 1).ToString() + "_3")).Text = R_Residual.ToString();
                    }
                }

                if (i >= 3)
                {
                    if (ShoveEL._Convert.StrToBool(dr[0]["HasSub"].ToString(), false))
                    {
                        NextID[i] = dr[0]["id"].ToString();
                        ((System.Web.UI.WebControls.Image)this.FindControl("imgNext" + (i + 1).ToString())).Visible = true;
                    }
                }
            }
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if(tbName.Text.Trim() == "")
        {
            ShoveEL._Web.JavaScript.Alert(this.Page,"请输入您在查找的会员");

            return;
        }

        User tu = new User();
        tu.Name = tbName.Text.Trim();

        string Return = "";
        int Result = tu.GetUserInfoByName(ref Return);

        if (Result < 0)
        {
            ShoveEL._Web.JavaScript.Alert(this.Page, "没有您要查找的会员");

            return;
        }

        // 检测是否是会员的下线        
        DataTable dt = DAL.Functions.F_GetUserNet.Open(_User.ID);

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["ID"].ToString() == tu.ID.ToString())
            {
                Server.Transfer("NetCopy.aspx?id=" + tu.ID);

                break;
            }
        }

        ShoveEL._Web.JavaScript.Alert(this.Page, "会员不是您的下线");

        return;
    }

}